package com.common.jedis;

import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * redis连接池
 * @Auther yanglei
 */

public class JedisPoolManager {
    private volatile static JedisPoolManager manager;
    private JedisPool pool;
    private JedisPoolManager(){
        InputStream in = null;
        try{
            //加载redis配置
            in =  JedisPoolManager.class.getClassLoader().getResourceAsStream("config"+File.separator+"env"+File.separator+"redis.properties");
            Properties props = new Properties();
            props.load(in);

            // 创建jedis池配置实例
            JedisPoolConfig config = new JedisPoolConfig();

            // 设置池配置项值
            String maxTotal = props.getProperty("redis.pool.maxTotal", "200");
            config.setMaxTotal(Integer.parseInt(maxTotal));

            String maxIdle = props.getProperty("redis.pool.maxIdle", "4");
            config.setMaxIdle(Integer.parseInt(maxIdle));

            String minIdle = props.getProperty("redis.pool.minIdle", "1");
            config.setMinIdle(Integer.parseInt(minIdle));

            String maxWaitMillis = props.getProperty("redis.pool.maxWaitMillis", "1024");
            config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));

            String testOnBorrow = props.getProperty("redis.pool.testOnBorrow", "true");
            config.setTestOnBorrow("true".equals(testOnBorrow));

            String testOnReturn = props.getProperty("redis.pool.testOnReturn", "true");
            config.setTestOnReturn("true".equals(testOnReturn));

            String host = props.getProperty("redis.host");
            if(StringUtils.isEmpty(host)){
                throw new IllegalArgumentException("JedisPool redis.host is empty!");
            }

            String port = props.getProperty("redis.port");
            if(StringUtils.isEmpty(port)){
                throw new IllegalArgumentException("JedisPool redis.port is empty!");
            }



            String master  =props.getProperty("redis.pool.master");
            if(StringUtils.isEmpty(master)){
                throw new IllegalArgumentException("JedisPool redis.pool.master is empty!");
            }

            String password = props.getProperty("redis.pool.password");
            if(StringUtils.isEmpty(password)){
                password=null;
            }

            // 根据配置实例化jedis池
            System.out.println("***********init JedisPool***********");
            //System.out.println("host->"+arr[0]+",port->"+arr[1]);

            pool = new JedisPool(config,host,Integer.parseInt(port),10000,password);
        }catch (IOException e){
            throw new IllegalArgumentException("init JedisPool error", e);
        }finally {
            try {
                if(null!=in){
                    in.close();
                }
            }catch (IOException ie){
                ie.printStackTrace();
            }

        }
    }

    public static JedisPoolManager getMgr() {
        if (manager == null) {
            synchronized (JedisPoolManager.class) {
                if (manager == null) {
                    manager = new JedisPoolManager();
                }
            }
        }
        return manager;
    }

    public Jedis getResource() {
        return pool.getResource();
    }

    public void destroy() {
        // when closing your application:
        pool.destroy();
    }

    public void close() {
        pool.close();
    }

    public static void returnResource(Jedis resource) {
        if (resource != null) {
            resource.close();
        }
    }
}
